package cn.biplam.front.component.view.systemManage.sysprivi.role.manage.privi;

import cn.biplam.common.ajxcode.SystemCode.RoleManageCode;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.RoleOptCode;
import cn.biplam.common.paramters.TreeParam;
import cn.biplam.common.paramters.UserPriviCaption;
import cn.biplam.common.struc.SysStruc.RoleIndexStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.ComponentBase;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Float_Type;
import cn.biplam.front.template.base.ButtonGroup;
import cn.biplam.front.template.buttons.AddButton;
import cn.biplam.front.template.control.FlatTable;
import cn.biplam.front.template.pageFrame.mdManage.MdObject;
import cn.biplam.front.template.pageFrame.mdManage.MdTable;

/**
 *  角色权限管理主页面
 * @author XiaoRuxing Dec 9, 20092:32:48 PM
 * @version 1.0
 */
public class RolePriviMain extends ComponentBase{

	public static final String cId = RoleManageCode.Role_Info+"list";

	public RolePriviMain(UserInfo user, String homeUrl, String recordId,
			KvMap<String, String> params) {
		super(user, homeUrl, recordId, params);
		if( params.getByKey(TreeParam.NODE_ID) != null){
			this.recordId = params.getByKey(TreeParam.NODE_ID);
		}
	}

	/**
	 * @return 角色权限管理主页面
	 * @throws ErrorMessage 
	 */
	public HtmlWindow get() throws ErrorMessage{
		MdObject mdo = new MdObject(cId,recordId,false);
		String domainId = mdo.getDomainId();
		String width = "500";//设置宽度为500

		//放入主内容区域
		MdTable table = new MdTable(domainId);
		//显示角色信息
		table.putObject(roleInfo(recordId, domainId, width));
		//显示角色所具有的权限
//		table.putObjectCol(rolePriviList(recordId, domainId, width));
		table.putObjectCol(rolePriviList(recordId, domainId, width), 
				UserPriviCaption.RolePrivl_List, 
				TOOLS.stringToInt(width));
		mdo.putTable(table.get());
		//放入操作按钮
		mdo.putOperator(operator());
		return mdo.get();
	}

	/**
	 * @return 角色基本信息组件
	 * @throws ErrorMessage 
	 */
	private HtmlWindow roleInfo(String recordId,String pageId,String width) throws ErrorMessage{
		//获取当前角色信息
		DataFilter filter = new DataFilter();
		filter.put(RoleIndexStruc.Role_ID, recordId);
		OperatorResult rs = Operator.get(RoleOptCode.Query_By_ID, mUser.getUserID(), filter);
		if(!TOOLS.exist(rs))
			return new HtmlDiv();
		//TODO 在已有权限时再添加权限，返回的rid是上级的，所以查询不到，需要查明原因并纠正
//			throw new ErrorMessage("Role do not exist!");
		String roleName = null;
		String roleDesc = null;
		roleName = rs.getValue(RoleIndexStruc.Role_Name);
		roleDesc = rs.getValue(RoleIndexStruc.Role_Desc);
		//创建角色信息显示控件
		FlatTable table= new FlatTable(pageId,recordId);
		table.putTh("名称", 0, 0);
		table.putTd(roleName, 0, 1);
		table.putTh("描述", 1, 0);
		table.putTd(roleDesc, 1, 1);
		table.setWidth(width);
		return table.get();
	}

	/**
	 * @return 角色权限列表
	 * @throws ErrorMessage 
	 */
	private HtmlWindow rolePriviList(String recordId,String pageId,String width) throws ErrorMessage{
		RolePriviList list = new RolePriviList();
		list.setRoleId(recordId);
		HtmlWindow win = list.get(mUser, sHomeUrl, cId);
		win.setWidth(width);
		return win;
	}

	/**
	 * @return 角色权限操作
	 * @throws ErrorMessage 
	 */
	private HtmlWindow operator() throws ErrorMessage{
		ButtonGroup operatorButtons=new ButtonGroup("",Float_Type.Left);
		if(recordId!=null){
			//添加角色权限
			AddButton editBt=new AddButton(
					mUser,
					cId+".addBt",
					RoleManageCode.Role_Privi_Add,
					recordId);
			editBt.setCaption(UserPriviCaption.Add_RolePrivi);
			operatorButtons.put(editBt.get());
		}
		return operatorButtons.get();
	}
}
